using System;
using System.Collections.Generic;
using System.Text;
using KaseyaWStestClient;
using KaseyaWStestClient.KaseyaWS;
using System.Xml;
using System.Xml.Serialization ;
using System.IO;

namespace Kaseya
{
    public class KaseyaWSClient
    {
        #region Private Data Members

        KaseyaWStestClient.KaseyaWS.KaseyaWS  KWS;
        
        #endregion

        #region Public Data Members
        
        public string SessionID;

        #endregion

        #region Constructors

        public KaseyaWSClient(string URL)
        {
            KWS = new KaseyaWStestClient.KaseyaWS.KaseyaWS();
            KWS.Url = URL;
        }

        #endregion

        #region Public Methods

        //Since the proxy objects created by our Web Reference
        // behave exactly like locally generated Business Entity Objects.
        //Note:: this is not a "Primative" method call.  It is the "Complex"
        //version, using object interface
        public string Authenticate(string UserName, string Password)
        {

            AuthenticationRequest req = new AuthenticationRequest();
            
            //call hash.dll here to encrypt password - ALL CLIENT CODE MUST USE hash.dll
            hash Hash = new hash();
            //password goes in by ref and will be modified by hash
            string RandomNumber = Hash.HashToCoveredPassword(UserName, ref Password);
            //set the unmodified username
            req.UserName = UserName;
            //set the hash generated single use encryption cover (2nd SHA-1 hash pass)
            req.RandomNumber = RandomNumber;
            //set the double SHA-1 hash encrypted password
            req.CoveredPassword = Password;

            AuthenticationResponse resp = KWS.Authenticate(req);
            SessionID = resp.SessionID.ToString();

            return SessionID;
            
        }

        public string ProcessRequest(string XmlIn)
        {
            //we always set the current SessionID, otherwise KaseyaWS security will fail the transaction
            string s = SetSessionID(XmlIn);

            // logic ladder identifying the method the xml is meant for.
            // This is reliable since WebMethods interfaces follow this pattern::
            // WebMethodName+Response WebMethodName(WebMethodName+Request) 
            if (s.Contains("<GetLogEntryRequest>"))
            {
                // For the "primative" methods, the primative datatype 'string' expresses the data 
                // and serialization / deserialization occurs on the web server.
                // We use "primative" here since the test app's UI is XML
                return KWS.PrimitiveGetLogEntry(s);

            }
            else if (s.Contains("<GetTicketListRequest>"))
            {
                return KWS.PrimitiveGetTicketList(s);
            }
            else if (s.Contains("<GetMachineListRequest>"))
            {
                return KWS.PrimitiveGetMachineList(s);
            }
            //else if (s.Contains("<CreateMachineGroupRequest>"))
           // {
           //     return KWS.PrimitiveCreateMachineGroup(s);
           // }
            else if (s.Contains("<GetMachineGroupListRequest>"))
            {
                return KWS.PrimitiveGetMachineGroupList(s);
            }
            else if (s.Contains("<GetMachineCollectionListRequest>"))
            {
                return KWS.PrimitiveGetMachineCollectionList(s);
            }
            else if (s.Contains("<GetAlarmListRequest>"))
            {
                return KWS.PrimitiveGetAlarmList(s);
            }
            else if (s.Contains("<GetAlarmRequest>"))
            {
                return KWS.PrimitiveGetAlarm(s);
            }
            else if (s.Contains("<GetTicketRequest>"))
            {
                return KWS.PrimitiveGetTicket(s);
            }
            else if (s.Contains("<GetMachineRequest>"))
            {
                return KWS.PrimitiveGetMachine(s);
            }
            else if (s.Contains("<CloseAlarmRequest>"))
            {
                return KWS.PrimitiveCloseAlarm(s);
            }
            else if (s.Contains("<UpdateTicketRequest>"))
            {
                return KWS.PrimitiveUpdateTicket(s);
            }
            else if (s.Contains("<CreateRoleRequest>"))
            {
                return KWS.PrimitiveCreateRole(s);
            }
            else if (s.Contains("<CreateRoleRequest>"))
            {
                return KWS.PrimitiveAssignRole(s);
            }
            else if (s.Contains("<GetRolesRequest>"))
            {
                return KWS.PrimitiveGetRoles(s);
            }
            else if (s.Contains("<AssignScopeRequest>"))
            {
                return KWS.PrimitiveAssignScope(s);
            }
            else if (s.Contains("<GetScopesRequest>"))
            {
                return KWS.PrimitiveGetScopes(s);
            }
            else if (s.Contains("<GetMachineGroupsRequest>"))
            {
                return KWS.PrimitiveGetMachineGroups(s);
            }
            else if (s.Contains("<AddOrgRequest>"))
            {
                return KWS.PrimitiveAddOrg(s);
            }
            else if (s.Contains("<GetOrgsRequest>"))
            {
                return KWS.PrimitiveGetOrgs(s);
            }
            else if (s.Contains("<ResetPasswordRequest>"))
            {
                return KWS.PrimitiveResetPassword(s);
            }
            else if (s.Contains("<UpdateUserRequest>"))
            {
                return KWS.PrimitiveUpdateUser(s);
            }
            else if (s.Contains("<AddScopeRequest>"))
            {
                return KWS.PrimitiveAddScope(s);
            }
            else if (s.Contains("<AddScopeOrgRequest>"))
            {
                return KWS.PrimitiveAddScopeOrg(s);
            }
            else if (s.Contains("<AddOrgToScopeRequest>"))
            {
                return KWS.PrimitiveAddOrgToScope(s);
            }
            else if (s.Contains("<AddUserToScopeRequest>"))
            {
                return KWS.PrimitiveAddUserToScope(s);
            }
            else if (s.Contains("<AddMachGroupToScopeRequest>"))
            {
                return KWS.PrimitiveAddMachGroupToScope(s);
            }
            else if (s.Contains("<AddUserToRoleRequest>"))
            {
                return KWS.PrimitiveAddUserToRole(s);
            }
            else if (s.Contains("<GetOrgsByScopeIDRequest>"))
            {
                return KWS.PrimitiveGetOrgsByScopeID(s);
            }
            return "Unknown XML";



        }

        public string SetSessionID(string payload)
        {

            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(payload);

            XmlNode xNode = xDoc.SelectSingleNode("//SessionID");

            if (xNode != null)
            {
                xNode.InnerXml = SessionID;
            }
            else
            {
                XmlNode newNode = xDoc.CreateElement("SessionID");
                newNode.InnerXml = SessionID;
                xDoc.DocumentElement.AppendChild(newNode);

            }

            return xDoc.OuterXml;

        }

        #endregion

        #region Private Methods

        

       

        #endregion
    }

    
}
